package com.xiaobawang.hundouluo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.xiaobawang.hundouluo.service.UserService;
import com.xiaobawang.hundouluo.utils.SharedPreferencesUtil;

public class PasswordChange extends AppCompatActivity implements View.OnClickListener {

    private EditText password, passwordVerify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_change);

        //完成userCheck的BroadCastPasswordChange的注册
        receiverPasswordChange = new PasswordChange.ChangePasswordReceiver();
        IntentFilter filterPasswordChange = new IntentFilter();
        filterPasswordChange.addAction("com.xiaobawang.hundouluo.receiver.changePassword");
        PasswordChange.this.registerReceiver(receiverPasswordChange, filterPasswordChange);

        findViewById(R.id.activity_password_change_btn_change).setOnClickListener(this);
        findViewById(R.id.activity_password_change_btn_cancel).setOnClickListener(this);

        password = findViewById(R.id.activity_password_change_et_password);
        passwordVerify = findViewById(R.id.activity_password_change_et_password);

    }

    BroadcastReceiver receiverPasswordChange;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销广播接收器
        PasswordChange.this.unregisterReceiver(receiverPasswordChange);
    }

    @Override
    public void onClick(View view) {

        String pwd = password.getText().toString().trim();
        String pwdVerify = passwordVerify.getText().toString().trim();

        switch (view.getId()){
            case R.id.activity_password_change_btn_change :
                if(pwd.length() == 0 || pwdVerify.length() == 0) {
                    Toast.makeText(PasswordChange.this, "请将信息填写完整!", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!(pwd.equals(pwdVerify))) {
                    Toast.makeText(PasswordChange.this, "两次输入密码不一致!", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Intent sIntent = new Intent(PasswordChange.this, UserService.class);
                    sIntent.putExtra("type", "changePassword");
                    String userName = SharedPreferencesUtil.read(PasswordChange.this, "userCheck", "uname");
                    sIntent.putExtra("username", userName);
                    sIntent.putExtra("password", pwd);
                    startService(sIntent);
                }
                break;

            case R.id.activity_password_change_btn_cancel :
                finish();
                break;
        }

    }

    public class ChangePasswordReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("changePasswordResult");
            if(result.equals("success")) {
                Toast.makeText(PasswordChange.this, "修改密码成功!", Toast.LENGTH_LONG).show();
                startActivity(new Intent(PasswordChange.this, LoginActivity.class));
            } else {
                Toast.makeText(PasswordChange.this, "修改密码失败!", Toast.LENGTH_LONG).show();
            }
        }
    }
}
